package com.example.tenmins;

import java.util.Random;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Multi extends Activity {
	Button btn[];
	String input_text;
	TextView num1, num2, input;
	Random generator;
	int n1, n2;
	int result;
	Toast mToast;
	LinearLayout linear1, linear2;
	Typeface font;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.multi);
	    // TODO Auto-generated method stub
	    
	    btn = new Button[12];
	    btn[0] = (Button)findViewById(R.id.btn1);
	    btn[1] = (Button)findViewById(R.id.btn2);
	    btn[2] = (Button)findViewById(R.id.btn3);
	    btn[3] = (Button)findViewById(R.id.btn4);
	    btn[4] = (Button)findViewById(R.id.btn5);
	    btn[5] = (Button)findViewById(R.id.btn6);
	    btn[6] = (Button)findViewById(R.id.btn7);
	    btn[7] = (Button)findViewById(R.id.btn8);
	    btn[8] = (Button)findViewById(R.id.btn9);
	    btn[9] = (Button)findViewById(R.id.btn10);
	    btn[10] = (Button)findViewById(R.id.btn11);
	    btn[11] = (Button)findViewById(R.id.btn12);
	    
	    btn[0].setOnClickListener(btnListener);
	    btn[1].setOnClickListener(btnListener);
	    btn[2].setOnClickListener(btnListener);
	    btn[3].setOnClickListener(btnListener);
	    btn[4].setOnClickListener(btnListener);
	    btn[5].setOnClickListener(btnListener);
	    btn[6].setOnClickListener(btnListener);
	    btn[7].setOnClickListener(btnListener);
	    btn[8].setOnClickListener(btnListener);
	    btn[9].setOnClickListener(btnListener);
	    btn[10].setOnClickListener(btnListener);
	    btn[11].setOnClickListener(btnListener);
	    
	    input_text = "";
	    
	    num1 = (TextView)findViewById(R.id.num1);
	    num2 = (TextView)findViewById(R.id.num2);
	    input = (TextView)findViewById(R.id.result);
	    
	    n1 = 0;
	    n2 = 0;
	    
	    generator = new Random();
	    n1 = generator.nextInt(9) + 1;
	    n2 = generator.nextInt(9) + 1;
	    
	    font = Typeface.createFromAsset(getAssets(), "font1.TTF");
	    num1.setTypeface(font, Typeface.BOLD);
	    num2.setTypeface(font, Typeface.BOLD);
	    
	    num1.setText(Integer.toString(n1));
	    num2.setText(Integer.toString(n2));
	    
	    result = n1 * n2;
	    
	    mToast = new Toast(Multi.this);
	    
	    linear1 = (LinearLayout)View.inflate(Multi.this, R.layout.correct, null);
	    linear2 = (LinearLayout)View.inflate(Multi.this, R.layout.incorrect, null);
	    
	    PopupActivity.at_m.add(this);
	}

	Button.OnClickListener btnListener = new Button.OnClickListener(){
		int n = 0;
		int length = 0;
		String str = "";
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch(arg0.getId()){
			case R.id.btn1:
				input_text += 1;
				input.setText(input_text);
				break;
			case R.id.btn2:
				input_text += 2;
				input.setText(input_text);
				break;
			case R.id.btn3:
				input_text += 3;
				input.setText(input_text);
				break;
			case R.id.btn4:
				input_text += 4;
				input.setText(input_text);
				break;
			case R.id.btn5:
				input_text += 5;
				input.setText(input_text);
				break;
			case R.id.btn6:
				input_text += 6;
				input.setText(input_text);
				break;
			case R.id.btn7:
				input_text += 7;
				input.setText(input_text);
				break;
			case R.id.btn8:
				input_text += 8;
				input.setText(input_text);
				break;
			case R.id.btn9:
				input_text += 9;
				input.setText(input_text);
				break;
			case R.id.btn11:
				input_text += 0;
				input.setText(input_text);
				break;
			case R.id.btn10:
				length = input.getText().toString().length();
				if(length == 0 || length == 1){
					str = "";
					input_text = "";
				}
				else {
					str = input.getText().toString().substring(0, length-1);
					input_text = str;
				}
				
				input.setText(str);
				break;
			case R.id.btn12:
				if(result == (n=Integer.parseInt(input.getText().toString()))){
					//맞았?�니??
					mToast.setView(linear1);
					mToast.show();
					for(int i = 0; i < PopupActivity.at_m.size(); i++)
						PopupActivity.at_m.get(i).finish();
				}
				else{
					//??��?�니??
					mToast.setView(linear2);
					mToast.show();
				}
				break;
			}
		}
		
	};
}
